/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mostafa
 */
public class InitialDataDeletion extends TransiantRemoval
{
    private String replicasFilePath ;
    private int noOfReplicas;
    private ArrayList<Double> meanTrajectory ;
    private ArrayList<Double> overallMean ;
    private ArrayList<Double> relativeChange ;
    private ArrayList<ArrayList<Double>>  data;

    public InitialDataDeletion(String replicasFilePath)
    {
        this.replicasFilePath = replicasFilePath;
        init();
    }
    private void init()
    {
        try
        {
            File file = new File(replicasFilePath);
            String [] replicaSNames = file.list();
            noOfReplicas = replicaSNames.length;
            // then start to load data from files
            data = new ArrayList<ArrayList<Double>>();
            for (int i = 0; i < replicaSNames.length; i++)
            {
                    String string = replicaSNames[i];
                    data.add(super.readFile(string));
            }
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(InitialDataDeletion.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(InitialDataDeletion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void calculateMeanTrajectory()
    {
        meanTrajectory = new ArrayList<Double>();
        for (int i = 0; i < NO_OF_RV; i++)
        {
            meanTrajectory.add((double) (1 / noOfReplicas) * getSum(i));
        }
    }

    private double getSum(int j)
    {
        double res = 0 ;
        for (int i = 0; i < data.size(); i++)
        {
            ArrayList<Double> list = data.get(i);
            res += list.get(j);
        }
        return res;
    }

    public void calculateRelativeChange()
    {
        calculateOverallMean();
        for (int i = 0; i < overallMean.size() - 1; i++)
        {
            double hi = overallMean.get(i + 1);
            double low = overallMean.get(i);
            relativeChange.add((double) (hi - low) / low);
        }
        try
        {
            // after all save data
            super.saveData("trajectory", meanTrajectory, 1);
            super.saveData("overallMean", overallMean, 0);
            super.saveData("relativeChange", relativeChange, 0);
        } catch (IOException ex)
        {
            Logger.getLogger(InitialDataDeletion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    private void calculateOverallMean()
    {
        overallMean = new ArrayList<Double>();
        double sum = 0;
        for (int i = 0; i < NO_OF_RV - 1; i++)
        {
            for (int j = i; j < NO_OF_RV; j++)
            {
                sum += meanTrajectory.get(j);
            }
            overallMean.add((double) (1 / (NO_OF_RV - i)) * sum);
            sum = 0;
        }
    }


    public static void main(String[] args)
    {
        
    }
}
